package jdbc.method;

import java.sql.*;


/**
 *
 * @author 顾书豪
 */
public class DBUtils {

    //驱动
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    //数据库的URL
    private static final String URL = "jdbc:mysql://localhost/lab?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8";
    //数据库的用户名
    private static final String USERNAME = "root";
    //数据库的密码
    private static final String PASSWORD = "123456";


    //数据库连接
    public Connection getConnection() {
        try {
            Class.forName(DRIVER);
            Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            System.out.println("Database connected.");
            return  conn;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return null;
    }


   /**
   * @Description: 用于注册用户
   * @Param: [userName, userPassword, phoneNumber, userRealName]
   * @return: boolean
   * @Author: Gu Shuhao
   * @Date: 2020/10/17
   */
    public boolean signUp(String userName, String userPassword, String phoneNumber, String userRealName){

        Connection con = this.getConnection();
        String sql = "insert into  account_information values(" + "\""+ userName + "\"" + "," + "\"" +userPassword + "\"" + "," +
                "\"" + phoneNumber +"\"" + "," + "\"" + userRealName + "\"" + "," + "0);";

        System.out.println(sql);
        try {
            Statement sta = con.createStatement();
            sta.executeUpdate(sql);
            return true;

        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return false;
    }

    /**
    * @Description: 用来向数据库中增加商品信息
    * @Param: [itemID, itemName, itemPrice, itemType, itemDescription, itemQuantity]
    * @return: boolean
    * @Author: Gu Shuhao
    * @Date: 2020/10/18
    */
    public boolean addItem(int itemID, String itemName, double itemPrice, String itemType,
                             String itemDescription, int itemQuantity){


        Connection con = this.getConnection();

        String sql = "insert into item_information values(" + itemID + "," + "\"" + itemName + "\"" + "," +
                itemPrice + "," + "\"" + itemType + "\"" + "," + "\"" + itemDescription + "\"" + "," + itemQuantity + ");";

        System.out.println(sql);
        try {
            Statement sta = con.createStatement();
            sta.executeUpdate(sql);
            return true;

        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return false;


    }

    /**
    * @Description: 添加商品图片
    * @Param: [itemId, picture]
    * @return: boolean
    * @Author: Gu Shuhao
    * @Date: 2020/10/18
    */
    public boolean addItemPicture(String itemId, String picture){

        Connection con = this.getConnection();
        String sql ="insert into item_picture values(" + "\"" + itemId + "\"" + "," + "\"" + picture +"\");";

        System.out.println(sql);
        try {
            Statement sta = con.createStatement();
            sta.executeUpdate(sql);
            return true;

        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return false;

    }

    /**
    * @Description: 添加顾客购买信息
    * @Param: [phoneNumber, itemId, itemQuantity]
    * @return: boolean
    * @Author: Gu Shuhao
    * @Date: 2020/10/18
    */
    public boolean addBuyInformation(String phoneNumber, String itemId, int itemQuantity){
        Connection con = this.getConnection();
        String sql ="insert into account_buy_information values(" + "\"" + phoneNumber + "\"" + ","
                + "\"" + itemId + "\"" + "," + itemQuantity +");";

        System.out.println(sql);
        try {
            Statement sta = con.createStatement();
            sta.executeUpdate(sql);
            return true;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    /**
    * @Description: 添加商家发布信息
    * @Param: [phoneNumber, itemId, itemQuantity]
    * @return: boolean
    * @Author: Gu Shuhao
    * @Date: 2020/10/18
    */
    public boolean addReleaseInformation(String phoneNumber, String itemId, int itemQuantity){

        Connection con = this.getConnection();
        String sql ="insert into account_release_information values(" + "\"" + phoneNumber + "\"" + ","
                + "\"" + itemId + "\"" + "," + itemQuantity + ");";

        System.out.println(sql);
        try {
            Statement sta = con.createStatement();
            sta.executeUpdate(sql);
            return true;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;

    }

    /**
    * @Description: 添加顾客收藏商品信息
    * @Param: [phoneNumber, itemID]
    * @return: boolean
    * @Author: Gu Shuhao
    * @Date: 2020/10/18
    */
    public boolean addItemCollect(String phoneNumber, String itemID){
        Connection con = this.getConnection();
        String sql ="insert into item_collect values(" + "\"" + phoneNumber + "\"" + "," + "\"" + itemID + "\");";

        System.out.println(sql);
        try {
            Statement sta = con.createStatement();
            sta.executeUpdate(sql);
            return true;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }





    /**
     * 查询数据
     */
    public void Query(String dataName, String tableName){

        Connection con = this.getConnection();

        String sql = "select" + dataName + "from" + tableName ;

    }



    public static void main(String [ ] args)
    {
        DBUtils db =new DBUtils();

        if(db.addItemCollect("111","1"));
            System.out.println("成功");

    }

}
